package com.ny.zmb.openapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.entity.PmProductOrder;
import com.ny.zmb.openapi.mapper.PmProductOrderMapper;
import com.ny.zmb.openapi.ysutil.YsHttpUtil;
import com.ny.zmb.openapi.ysutil.base.EYinShengBase;
import com.ny.zmb.openapi.ysutil.common.ServiceEnum;
import com.ny.zmb.openapi.ysutil.from.BizReqJsonCollege.BizReqJson;
import com.ny.zmb.openapi.ysutil.from.BizReqJsonCollege.BizReqJsonRefund;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;


/**
 * YS 交易订单
 *
 * @author ChenZhiZheng
 * @date 20241106
 */

@RestController
@CrossOrigin
@RequestMapping("/ys/order")
public class YsPayOrderController extends EYinShengBase {

    @Resource
    private YsHttpUtil ysHttpUtil;
    @Resource
    private PmProductOrderMapper pmProductOrderMapper;

    @GetMapping("getInfoByNo")
    public Result getInfoByNo(String ordersNo){
        QueryWrapper<PmProductOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", ordersNo);
        return Result.ok(Optional.ofNullable(pmProductOrderMapper.selectOne(queryWrapper))
                .orElseThrow(()->new RuntimeException("订单不存在")));

    }

    /**
     * 订单API
     *
     * @param srcMerchantNo 商户号
     * @param obj           请求体
     * @param methodStr     方法
     * @return Result
     */
    @PostMapping("/{methodStr}")
    public Result refundPost(@RequestBody BizReqJsonRefund obj, @RequestParam(required = true) String srcMerchantNo, @PathVariable String methodStr) {
        ServiceEnum anyEnum;
        // 调用校验方法
        switch (methodStr.trim()) {
            default:
                throw new RuntimeException("地址错误");
        }

    }

    @GetMapping("/{methodStr}")
    public Result refundGet(@RequestBody BizReqJson obj, @RequestParam(required = true) String srcMerchantNo, @PathVariable String methodStr) {
        ServiceEnum anyEnum;
        // 调用校验方法
        switch (methodStr.trim()) {
            // 交易退款订单查询
            case "queryTradeRefund":
                // 回调
                anyEnum = ServiceEnum.QUERY_TRADE_REFUND_ORDER;
                break;
            // 交易订单查询
            case "queryUnifyOrder":
                // 回调
                anyEnum = ServiceEnum.QUERY_UNIFY_ORDER;
                break;
            default:
                throw new RuntimeException("地址错误");
        }
        try {
            JSONObject result = ysHttpUtil.sendBase(anyEnum, "prd", MERCHANT_PRX, MERCHANT_PRX_PWD, methodStr, srcMerchantNo, obj);
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }

    }
}
